package com.iknight.theory.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iknight.theory.api.TeLineConsService;
import com.iknight.theory.mapper.TeLineConsMapper;
import com.iknight.theory.model.TELineConsModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TeLineConsServiceImpl implements TeLineConsService<TELineConsModel, String> {
    @Autowired
    private TeLineConsMapper<TELineConsModel, String> teLineConsMapper;

    @Override
    public void save(TELineConsModel record) {
        if (null == teLineConsMapper.selectOne(record.getCl_id())) {
            teLineConsMapper.insert(record);
        } else {
            teLineConsMapper.update(record);
        }
    }

    @Override
    public void delete(String pkey) {
        teLineConsMapper.delete(pkey);
    }

    @Override
    public TELineConsModel selectOne(String pkey) {
        return teLineConsMapper.selectOne(pkey);
    }

    @Override
    public PageInfo<TELineConsModel> selectPage(Map<String, Object> params, int pageStart, int pageSize) {
//        return null;
        PageHelper.startPage(pageStart, pageSize, true);
        List<TELineConsModel> result = teLineConsMapper.selectByCond(params);
        PageInfo<TELineConsModel> page = new PageInfo<>(result);
        return page;
    }

    @Override
    public void batchSave(List<TELineConsModel> entities) {
        for (TELineConsModel entity : entities) {
            this.save(entity);
        }
    }

    @Override
    public List<Map> getLineConsByLineId(String line_id) {
        return teLineConsMapper.getLineConsByLineId(line_id);
    }

    @Override
    public PageInfo<Map> getConsLinessByParam(Map<String, Object> param) {
        int pageSize = Integer.parseInt(param.get("pagesize").toString());
        int pageStart = Integer.parseInt(param.get("pagestart").toString());
        PageHelper.startPage(pageStart, pageSize, true);
        List<Map> result = teLineConsMapper.getConsLinessByParam(param);
        return new PageInfo<>(result);
    }
}
